/*
 * File:    FreeList.cpp
 * Authors: Diane, Dan, and Tim
 * Author:  Chris Prince for DOxygen Functions and Comments
 * Class:   Operating Systems, Lab 2, UMD
 * Created: October 2012
 * 
 * Main algorithms for creating FreeList, a special BlockLinkedList.
 * FreeList keeps track of unused blocks on the Disk device.
 */

#include "FreeList.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <unistd.h> // Mac OS X
//include whatever

FreeList::FreeList(){
    masterBlock = NULL;
}

FreeList::FreeList(Disk*disk, bool createFreeList) {
      
    if(createFreeList)
    {
        printf("FreeList:  ERROR:  Disk format failed\n");
    }
    
    else
        printf("FreeList:  SUCCESS:  Disk format\n");
    
    //masterBlock point to first block 
    masterBlock = new Block(0);
    masterBlock->setPointer(1, Block::NEXT_POINTER);
    
    if(!masterBlock->write(disk))
    {
        printf("FreeList:  ERROR:  masterBlock write failed\n")
        
    }
    else 
        printf("FreeList:  SUCCESS:  masterBlock write\n");
    
    //set pointer to current block
    Block *curBlock;
    
    //iterate through rest of block
    // write list of empty blocks
    for(int i=1; i < FreeList::DEFAULT_NUMBER_OF_BLOCKS; i++)
    {
        if(curBlock)
            curBlock = new Block(i)
            curBlock->setPointer(i+1, Block::NEXT_POINTER);
        if(curBlock->write(disk))
        {
            cout << "block" << i << " write succeeded\n";
        }
    }
    
    }

//Writes out the current start block of the free list to block 0
bool FreeList::Close() {
    
    //if close is unsuccessful, return false
    if(close(curBlock) == -1)
    {
        printf("FreeList.Close: ERROR:  Could not close file\n");
        return false;
    }
    //if close is successful, return true
    printf("FreeList.Close:  SUCCESS: Closed file!\n");
    return true;

    }

//Create a new BlockGroup from FreeList
BlockGroup*FreeList::CreateNew() {
    }

//Returns all blocks of BlockLinkedList to FreeList
void FreeList::Return(BlockLinkedList*bll) {

    }

//Sanity checks on block numbers.
int FreeList::TestBlockGroup(int startBlockNumber, FreeList*mother) {

    }


FreeList::~FreeList() {
    }
